import pymysql

class DBUtils(object):
    def connDB(self):                              #连接数据库
        conn=pymysql.connect(host='192.168.251.114',port=3306, user='root',passwd='b6f3g2',db='yangsj',charset='utf8');
        cur=conn.cursor();
        return (conn,cur);

    def exeUpdate(self,conn,cur,sql):                #更新或插入操作
        sta=cur.execute(sql);
        conn.commit();
        return (sta);

    def exeDelete(self,conn,cur,IDs):                #删除操作 demo 没用到
        sta=0;
        for eachID in IDs.split(' '):
            sta+=cur.execute("delete from students where Id=%d"%(int(eachID)));
        conn.commit();
        return (sta);

    def exeQuery(self,cur,sql):                      #查找操作
        effect_row = cur.execute(sql);
        return (effect_row,cur);

    def connClose(self,conn,cur):                    #关闭连接，释放资源
        cur.close();
        conn.close();

if __name__ == '__main__':
    dbUtil = DBUtils();
    conn,cur = dbUtil.connDB();
